using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;
using System.IO.IsolatedStorage;
using System.IO;
using System.Windows.Forms.VisualStyles;
using System.Xml;


namespace KbsUmlEditor
{
    public partial class XMLUML : Form
    {
        # region members
        System.IO.StreamReader fileToPrint;
        System.Drawing.Font printFont;


        //samenvoeging jan

        String m_FileName;

        XmlReaderSettings m_Settings;

        XmlDocument m_XDoc;
        XmlNode m_Root;

        UMLDiagram m_Diagram;



        //samenvoeging manuel
       
        public static  int gridFactor = 10;

        # endregion

        public XMLUML()
        {
            InitializeComponent();
        }

        # region Mainmenu
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Diagram bewaren?", "Save", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                XMLparser.SaveDiagramToXml(m_FileName, m_Diagram);
            }
            Application.Exit();

        }

        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Diagram bewaren?", "Save", MessageBoxButtons.OKCancel) == DialogResult.OK) 
            {
                XMLparser.SaveDiagramToXml(m_FileName, m_Diagram);
            }
            
               
            m_Diagram = new UMLDiagram("");

            treeView1.Nodes.Clear();
            


        }


        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {

            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            //openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "xml files (*.xml)|*.xml";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;


            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    
                    treeView1.Refresh();
                    m_FileName = openFileDialog1.FileName;

                    m_Settings = new XmlReaderSettings();
                    m_Settings.IgnoreWhitespace = true;

                    //m_Reader = XmlReader.Create(m_FileName, m_Settings);

                    m_XDoc = new XmlDocument();
                    m_XDoc.Load(m_FileName);
                    m_Root = m_XDoc.DocumentElement;


                    //ParseObjects();
                    //BuildTree();

                    //Peter01032009
                    m_Diagram = XMLparser.ParseObjects(treeView1, m_Diagram, m_Settings, m_FileName);

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            XMLparser.SaveDiagramToXml(m_FileName, m_Diagram);
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.InitialDirectory = "c:\\";
            saveFileDialog1.Filter = "xml files (*.xml)|*.xml";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {

                    XMLparser.SaveDiagramToXml(saveFileDialog1.FileName, m_Diagram);

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
                }
            }
        }

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (m_FileName != null)
            {
                printDialog1.ShowDialog();
                string printPath = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                //fileToPrint = new System.IO.StreamReader(currentfile);
                fileToPrint = new System.IO.StreamReader(m_FileName);

                printFont = new System.Drawing.Font("Arial", 10);
                printDocument1.Print();
                fileToPrint.Close();
            }
            else
            {
                MessageBox.Show("Er is geen file geopend!");
            }
        }

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            float yPos = 0f;
            int count = 0;
            float leftMargin = e.MarginBounds.Left;
            float topMargin = e.MarginBounds.Top;
            string line = null;
            float linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
            while (count < linesPerPage)
            {
                line = fileToPrint.ReadLine();
                if (line == null)
                {
                    break;
                }
                yPos = topMargin + count * printFont.GetHeight(e.Graphics);
                e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
                count++;
            }
            if (line != null)
            {
                e.HasMorePages = true;
            }
        }

        private void manuelToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //bmpDesign = new Bitmap(pic_design.Width, pic_design.Height);
            //Graphics.FromImage(bmpDesign).Clear(Color.White);
            //pic_design.Image = (Bitmap)bmpDesign.Clone();

            //Pijl p1 = new Pijl(new Point(30, 50), new Point(30, 150));
            //p1.draw(pic_design.Image, gridFactor);

            //Pijl p2 = new Pijl(new Point(50, 50), new Point(80, 250));
            //p2.PuntStart = new Punt(0, Direction.North);
            //p2.PuntEnd = new Punt(0, Direction.East);
            //p2.draw(pic_design.Image, gridFactor);

            //Pijl p3 = new Pijl(new Point(100, 50), new Point(180, 200));
            //p3.Breek = true;
            //p3.PuntStart = new Punt(0, Direction.North);
            //p3.PuntEnd = new Punt(0, Direction.North);
            //p3.draw(pic_design.Image, gridFactor);
        }

        private void testToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_Diagram = new UMLDiagram("test");
            Point begin = new Point(2, 2);
            Point eind = new Point(60, 60);
            UMLKlasse klasse = new UMLKlasse(begin, eind, "testklasse");

            begin = new Point(29, 61);
            eind = new Point(29, 100);
            UMLLijn lijn = new UMLLijn(begin, eind, UMLPunt.Type.association, UMLPunt.Type.association);

            begin = new Point(2, 100);
            eind = new Point(60, 160);
            UMLNotitie notitie = new UMLNotitie(begin, eind);
            notitie.Notitie = "Notite test";

            m_Diagram.VoegToeUMLObject(klasse);
            m_Diagram.VoegToeUMLObject(lijn);
            m_Diagram.VoegToeUMLObject(notitie);



            panel2.Invalidate();
        }

        private void zoomToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //m_Diagram.SetFactor(4);
            //panel2.Invalidate();
        }

        private void printUMLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_Diagram.printDrawing(panel2);
        }

        private void toolStripMenuItem50Procent_Click(object sender, EventArgs e)
        {
            //zoom 50%
            m_Diagram.SetFactor(0.5);
            panel2.Invalidate();
        }

        private void toolStripMenuItem100Procent_Click(object sender, EventArgs e)
        {
            //zoom 100%
            m_Diagram.SetFactor(1);
            panel2.Invalidate();
        }


        private void toolStripMenuItem200Procent_Click(object sender, EventArgs e)
        {
            //zoom 200%
            m_Diagram.SetFactor(2);
            panel2.Invalidate();

        }

        private void jpegToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_Diagram.exportDrawing(panel2, "Jpeg");
        }

        private void gifToolStripMenuItem_Click(object sender, EventArgs e)
        {
            m_Diagram.exportDrawing(panel2, "Gif");
        }
        #endregion

        # region treeview


        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try{

            int level = treeView1.SelectedNode.Level;


            int selectednode = GetSelectedNodeNr();

            m_Diagram.UMLObjectsList.RemoveAt(selectednode - 1);
            //BuildTree();
            XMLparser.BuildTree(treeView1, m_Diagram);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);

            }

        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EditNode();


        }

        private void classToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            NewUmlClass();
        }

        private void lineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewUmlLine();
        }

        private void notitieToolStripMenuItem_Click(object sender, EventArgs e)
        {
            NewUmlNotitie();

        }



        private void EditNode()
        {

            try
            {
                int level = treeView1.SelectedNode.Level;


                int selectednode = GetSelectedNodeNr();

                UMLObject umlobject = m_Diagram.UMLObjectsList[selectednode - 1];
                //klasse
                if (umlobject.GetType() == typeof(UMLKlasse))
                {
                    UMLKlasse klasse = (UMLKlasse)umlobject;
                    FormKlasseEdit edit = new FormKlasseEdit();

                    edit.Naam = klasse.Naam;
                    edit.Begin = klasse.BeginPositie;
                    edit.Eind = klasse.EindPositie;

                    edit.ShowDialog();

                    if (edit.DialogResult == DialogResult.OK)
                    {
                        klasse.Naam = edit.Naam;
                        klasse.BeginPositie = edit.Begin;
                        klasse.EindPositie = edit.Eind;
                    }



                }
                //lijn
                if (umlobject.GetType() == typeof(UMLLijn))
                {
                    UMLLijn lijn = (UMLLijn)umlobject;
                    FormLijnEdit editlijn = new FormLijnEdit();

                   // editlijn.Naam = lijn.Naam;
                    editlijn.Begin = lijn.BeginPositie;
                    editlijn.Eind = lijn.EindPositie;
                    editlijn.BeginPuntType = lijn.BeginPunt.ToString();
                    editlijn.EindPuntType = lijn.EindPunt.ToString();
                    editlijn.ShowDialog();

                    if (editlijn.DialogResult == DialogResult.OK)
                    {
                        //lijn.Naam = editlijn.Naam;
                        lijn.BeginPositie = editlijn.Begin;
                        lijn.EindPositie = editlijn.Eind;
                        lijn.BeginPunt = UMLPunt.getType(editlijn.BeginPuntType);
                        lijn.EindPunt = UMLPunt.getType(editlijn.EindPuntType);
                    }



                }

                //notitie
                if (umlobject.GetType() == typeof(UMLNotitie))
                {
                    UMLNotitie notitie = (UMLNotitie)umlobject;
                    FormNotitiEdit editnotitie = new FormNotitiEdit();

                    // editlijn.Naam = lijn.Naam;
                    editnotitie.Begin = notitie.BeginPositie;
                    editnotitie.Eind = notitie.EindPositie;
                    editnotitie.Notitie = notitie.Notitie;
                    editnotitie.ShowDialog();
                    if (editnotitie.DialogResult == DialogResult.OK)
                    {

                        //lijn.Naam = editlijn.Naam;
                        notitie.BeginPositie = editnotitie.Begin;
                        notitie.EindPositie = editnotitie.Eind;
                        notitie.Notitie = editnotitie.Notitie;

                    }

                }

                //BuildTree();
                XMLparser.BuildTree(treeView1, m_Diagram);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }

        private int GetSelectedNodeNr()
        {
            int i = 0;
            foreach (TreeNode node in treeView1.Nodes[0].Nodes)
            {
                if (node.IsSelected)
                {
                    return i;
                }
                foreach (TreeNode nodechild in node.Nodes)
                {
                    if (nodechild.IsSelected)
                    {
                        return i;
                    }
                    foreach (TreeNode nodegrantchild in nodechild.Nodes)
                    {
                        if (nodegrantchild.IsSelected)
                        {
                            return i;
                        }
                    }
                }
                i++;
            }
            return -1;
        }

        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            EditNode();
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                treeView1.SelectedNode = treeView1.GetNodeAt(e.Location);
            }
        }

        #endregion

        # region NewObjects

        private void NewUmlClass()
        {

            //klasse
            FormKlasseEdit edit = new FormKlasseEdit();

            edit.Naam = "";
            edit.Begin = Point.Empty;
            edit.Eind = Point.Empty;

            edit.ShowDialog();
            if (edit.DialogResult == DialogResult.OK)
            {
                UMLKlasse klasse = new UMLKlasse(edit.Begin, edit.Eind, edit.Naam);
                m_Diagram.VoegToeUMLObject(klasse);


                //BuildTree();
                XMLparser.BuildTree(treeView1, m_Diagram);
            }
        }

        private void NewUmlLine()
        {

            //klasse
            FormLijnEdit editlijn = new FormLijnEdit();

            editlijn.Naam = "";
            editlijn.Begin = Point.Empty;
            editlijn.Eind = Point.Empty;
            editlijn.BeginPuntType = "";
            editlijn.EindPuntType = "";
            editlijn.ShowDialog();

            if (editlijn.DialogResult == DialogResult.OK)
            {
                UMLLijn lijn = new UMLLijn(editlijn.Begin, editlijn.Eind, UMLPunt.Type.association, UMLPunt.Type.association);
                m_Diagram.VoegToeUMLObject(lijn);


                //BuildTree();
                XMLparser.BuildTree(treeView1, m_Diagram);
            }
        }

        private void NewUmlNotitie()
        {
            //klasse
            FormNotitiEdit editnotitie = new FormNotitiEdit();

            editnotitie.Notitie = "";
            editnotitie.Begin = Point.Empty;
            editnotitie.Eind = Point.Empty;
       
            editnotitie.ShowDialog();

            if (editnotitie.DialogResult == DialogResult.OK)
            {
                UMLNotitie notitie = new UMLNotitie(editnotitie.Begin, editnotitie.Eind);
                notitie.Notitie = editnotitie.Notitie;
                m_Diagram.VoegToeUMLObject(notitie);


                //BuildTree();
                XMLparser.BuildTree(treeView1, m_Diagram);
            }
        }
        
        # endregion

        # region panel2
        private Point b;
        private Point n;


        public void panel2_Paint(object klasse, PaintEventArgs e)
        {

            //Teken de klasse
            //klas.Teken(e.Graphics);
            bool grid = true;
            if (grid)
            {
                DrawGrid(e.Graphics);
            }
            if (m_Diagram != null)
            {
                m_Diagram.Grid = gridFactor;
                m_Diagram.Teken(e.Graphics);
            }


           


        }
        private void DrawGrid(Graphics g)
        {
            //horz
            for (int i = 0; i < panel2.Height; i += gridFactor)
            {
                System.Drawing.Pen myPen;
                myPen = new System.Drawing.Pen(System.Drawing.Color.FromArgb(50,Color.LightGray));
                g.DrawLine(myPen, 0, i, panel2.Width, i);
            }
            //vert
            for (int i = 0; i < panel2.Width; i += gridFactor)
            {
                System.Drawing.Pen myPen;
                myPen = new System.Drawing.Pen(System.Drawing.Color.FromArgb(50, Color.LightGray));
                g.DrawLine(myPen, i, 0, i, panel2.Height);
            }
        }
        private void panel2_MouseClick(object sender, MouseEventArgs e)
        {
            panel2.Invalidate();
        }

        # endregion

        private void panel2_MouseDown(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                int f1 = e.X / gridFactor;
             int f2 = e.Y/gridFactor;
                Font myFont = new Font(FontFamily.GenericSansSerif, 8, FontStyle.Regular);
                 string pos = "pos: "+f1.ToString()+ "/" + f2.ToString();
                Graphics g = panel2.CreateGraphics();
                g.DrawString(pos, myFont,
                Brushes.Firebrick, e.X, e.Y);
                

            }

        }

        private void panel2_MouseMove(object sender, MouseEventArgs e)
        {
            
        }

        
       




    }
}
